package com.hunan.api.catv.mq.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.entity.po.News;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wangchao
 * @Date: 2020/11/24 15:55
 */
@Component
@Slf4j
public class ClearNewsInfoConsumer {

    @Autowired
    RedisUtils redisUtils;


    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.CLEAR_NEWS_INFO_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.CLEAR_NEWS_INFO_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.CLEAR_NEWS_INFO_ROUTINGKEY))
    public void tasks(String memberId) {
        log.info("[清除该用户下发布短视频缓存:{}]", memberId);
        //查询用户发布的短视频列表
        List<News> newsList = new News().selectList(new QueryWrapper<News>().eq(News.MEMBER_ID, memberId));
        newsList = newsList.parallelStream().map(news -> {
            redisUtils.del(MessageFormat.format(CacheName.getNewsInfo, String.valueOf(news.getNewsId())));
            return news;
        }).collect(Collectors.toList());
    }


}
